﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using SVM;

namespace Yah.Parser
{
    public class VectorArrti
    {
        public List<int> Values = new List<int>();

        public VectorArrti(int n)
        {
            Values = new List<int>();
            for (var i = 0; i <= n; i++)
            {
                Values.Add(0);
            }
        }

        public string GetVector()
        {
            var res = "";
            var i = 0;
            foreach (var attri in Values)
            {
                if (i > 0)
                {
                    res += i.ToString(CultureInfo.InvariantCulture);
                    res += ":";
                    res += attri.ToString(CultureInfo.InvariantCulture);
                    res += " ";
                }
                else
                {
                    res += attri.ToString(CultureInfo.InvariantCulture);
                    res += " ";
                }
                i++;
            }
            return res.Trim();
        }

        public string ViewVector()
        {
            var res = "";
            var i = 0;
            foreach (var attri in Values)
            {
                if (i > 0)
                {
                    if (attri != 0)
                    {
                        res += i.ToString(CultureInfo.InvariantCulture);
                        res += ":";
                        res += attri.ToString(CultureInfo.InvariantCulture);
                        res += " ";
                    }
                }
                else
                {
                    res += attri.ToString(CultureInfo.InvariantCulture);
                    res += " ";
                }
                i++;
            }
            return res.Trim();
        }

        public static VectorArrti Create(string[] s)
        {
            var res = new VectorArrti(s.Count());
            for (var i = 1; i < res.Values.Count; i++)
            {
                res.Values[i] = (Convert.ToInt32(s[i].Substring(s[i].LastIndexOf(':') + 1)));
            }
            return res;
        }

        public Node[] GetNode()
        {
            var res = new Node[Values.Count - 1];
            for (var i = 1; i < Values.Count; i++)
            {
                var node = new Node(i, Values[i]);
                res[i - 1] = node;
            }
            return res;
        }
    }
}